Page({
  data: {
    id: null,
    product: null,
    loading: true,
    selectedImage: 0, // 选中的图片索引
    selectedTab: 0, // 选中的标签页索引
    selectedSpecs: {}, // 选中的规格
    images: [
      "/image/banner/banner1.jpg",
      "/image/banner/banner2.jpg",
      "/image/banner/banner3.jpg"
    ],
    productInfo: {
      title: "精品萨摩耶幼犬 纯种萨摩耶 微笑天使 多只可挑选",
      price: "3800.00",
      originalPrice: "4500.00",
      sales: 129,
      stock: 38,
      description: "萨摩耶犬，又称萨莫耶德犬，具有天使般雪白的被毛，一个真诚的笑容以及一对充满智慧的眼睛。它们极其人性化的性格使其成为优秀的宠物狗。它们友好、忠诚而且适应性极强。萨摩耶德犬是游牧民族萨莫耶德人饲养的犬种，原始的用处是拉雪橇和保护主人的雪橇狗。",
      details: [
        "品种：萨摩耶",
        "年龄：2-4个月",
        "性别：公母均有",
        "毛色：纯白",
        "体型：中大型",
        "疫苗：已做2次",
        "驱虫：已做内外驱虫"
      ],
      specifications: [
        {
          name: "年龄",
          values: ["2个月", "3个月", "4个月"]
        },
        {
          name: "性别",
          values: ["公", "母"]
        }
      ]
    },
    // 评价数据
    comments: [
      {
        id: 1,
        avatar: "/image/banner/banner1.jpg",
        username: "张**",
        rating: 5,
        content: "狗狗很健康，非常活泼可爱，卖家服务很好，物流也很快",
        date: "2023-09-15",
        images: ["/image/banner/banner1.jpg", "/image/banner/banner2.jpg"]
      },
      {
        id: 2,
        avatar: "/image/banner/banner2.jpg",
        username: "李**",
        rating: 5,
        content: "纯种萨摩耶，超级可爱，白白的毛，笑容太治愈了",
        date: "2023-09-10",
        images: ["/image/banner/banner3.jpg"]
      }
    ],
    // 推荐商品
    recommendations: [
      {
        id: 101,
        image: "/image/banner/banner2.jpg",
        title: "柯基幼犬 威尔士柯基犬 两色三色均有",
        price: "4200"
      },
      {
        id: 102,
        image: "/image/banner/banner3.jpg",
        title: "布偶猫活体 幼猫纯种 可爱粘人",
        price: "5500"
      },
      {
        id: 103,
        image: "/image/banner/banner1.jpg",
        title: "金毛寻回犬 忠诚友善 家庭伴侣犬",
        price: "2800"
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取商品ID
    const id = options.id;
    
    // 显示加载状态
    this.setData({
      id: id,
      loading: true
    });
    
    // 网络请求延迟
    setTimeout(() => {
      const product = this.getProductById(id);
      
      // 初始化规格选择
      const selectedSpecs = {};
      if (product.specifications && product.specifications.length > 0) {
        product.specifications.forEach(spec => {
          if (spec.values && spec.values.length > 0) {
            selectedSpecs[spec.name] = '';
          }
        });
      }
      
      // 使用nextTick确保数据在渲染前准备好
      wx.nextTick(() => {
        this.setData({
          loading: false,
          product: product,
          selectedSpecs: selectedSpecs
        });
      });
    }, 500); 
  },

  // 根据ID获取商品详情
  getProductById: function(id) {
    return {
      id: id,
      title: this.data.productInfo.title,
      price: this.data.productInfo.price,
      originalPrice: this.data.productInfo.originalPrice,
      sales: this.data.productInfo.sales,
      stock: this.data.productInfo.stock,
      images: this.data.images,
      description: this.data.productInfo.description,
      details: this.data.productInfo.details,
      specifications: this.data.productInfo.specifications
    };
  },

  // 返回上一页
  onBack: function() {
    wx.navigateBack();
  },

  // 切换大图
  changeImage: function(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      selectedImage: index
    });
  },
  
  // 轮播图变化事件
  onSwiperChange: function(e) {
    const current = e.detail.current;
    this.setData({
      selectedImage: current
    });
  },

  // 切换标签页
  changeTab: function(e) {
    const index = parseInt(e.currentTarget.dataset.index);
    if (this.data.selectedTab === index) {
      return; 
    }
    
    this.setData({
      selectedTab: index
    });
    
    console.log('切换到标签页:', index);
  },

  // 选择规格
  selectSpec: function(e) {
    const { specName, value } = e.currentTarget.dataset;
    const selectedSpecs = this.data.selectedSpecs;
    
    // 更新选中的规格
    selectedSpecs[specName] = value;
    
    this.setData({
      selectedSpecs: selectedSpecs
    });
    

  },

  // 查看大图
  previewImage: function(e) {
    const current = e.currentTarget.dataset.src;
    const urls = this.data.product.images;
    
    wx.previewImage({
      current: current,
      urls: urls
    });
  },
  
  // 预览评论图片
  previewCommentImage: function(e) {
    const current = e.currentTarget.dataset.current;
    const urls = e.currentTarget.dataset.urls;
    
    // 确保URLs是字符串数组
    const imageUrls = Array.isArray(urls) ? urls : [current];
    
    wx.previewImage({
      current: current,
      urls: imageUrls,
      showmenu: true, // 显示预览菜单
      success: function() {
        console.log('预览评论图片成功');
      },
      fail: function(error) {
        console.error('预览评论图片失败:', error);
      }
    });
  },

  // 加入购物车
  addToCart: function() {
    wx.showToast({
      title: '已加入购物车',
      icon: 'success'
    });
  },

  // 立即购买
  buyNow: function() {
    wx.showToast({
      title: '购买功能开发中',
      icon: 'none'
    });
  },

  // 查看评价详情
  viewMoreComments: function() {
    wx.showToast({
      title: '评价详情功能开发中',
      icon: 'none'
    });
  },

  // 查看推荐商品详情
  onRecommendTap: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/product/product?id=${id}`
    });
  }
}); 